import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/stock_quant_odoo_api.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'data_grid.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ProductQuant extends StatefulWidget {
  ProductQuant({Key? key}) : super(key: key);

  @override
  _ProductQuantState createState() => _ProductQuantState();
}

class _ProductQuantState extends State<ProductQuant> {
  List<StockQuant> ylkData = [];
  List<StockQuant> xbkData = [];
  List<StockQuant> cpkData = [];
  int length = 0;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    ylkData = await getData("YLK", 20);
    xbkData = await getData("XBK", 10);
    cpkData = await getData("CPK", 10);
    setState(() {});
  }

  Future<List<StockQuant>> getData(String comment, int limit) async {
    List<StockQuant> result = [];
    ResponseBodyApi res =
        await StockQuantOdooApi.getDataByComment(comment, limit);
    if (res.success!) {
      res.data.forEach((e) {
        Map<String, dynamic> map = e;
        result.add(StockQuant.fromMap2(map));
      });
    }
    return result;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/bg/bg1.png"),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.all(10.r),
              child: Text("产品库存管理",style: TextStyle(
                  fontSize: 36.sp,
                  decoration: TextDecoration.none,
                  color: Color.fromRGBO(255, 255, 255, 1)
              ),),
            ),
            Expanded(
              child: Row(
                children: [
                  ylkData.length == 0
                      ? Container()
                      : Expanded(
                          child: DataGtid(
                            dataList: ylkData,
                            title: "原料库产品库存状况统计",
                            scrollBars: 5,
                          ),
                        ),
                  (xbkData.length == 0 && cpkData.length == 0)
                      ? Container()
                      : Expanded(
                          child: Column(
                            children: [
                              xbkData.length == 0
                                  ? Container()
                                  : Expanded(
                                      child: DataGtid(
                                        dataList: xbkData,
                                        title: "线边库库产品库存状况统计",
                                        scrollBars: 3,
                                      ),
                                    ),
                              cpkData.length == 0
                                  ? Container()
                                  : Expanded(
                                      child: DataGtid(
                                        dataList: cpkData,
                                        title: "成品库产品库存状况统计",
                                        scrollBars: 3,
                                      ),
                                    ),
                            ],
                          ),
                        ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
